library(tidyverse)
library(magrittr)
library(readr)
library(readxl)
library(ggthemes)
library(foreign)

## Change wd
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
rm(list=ls())
gc()

# Data ----

## Coalition management in Brazil data ----
coalmngm_brazil <- 
  read_excel("data/dataset_coalmngm_brazil_v8.xlsx",
             col_names = TRUE, skip = 6) %>%
  dplyr::filter(is.na(term)==F) %>%
  # Mutating and correcting lots of variables  
  mutate(date = as.character(date), # Making dates readable
         date = str_pad(date, 7, pad = "0"), #padding zeros to correct
         date_yearmon = as.yearmon(date,format="%m %Y"), # Using package to have years and months
         # bulding complete dates with days because R doesn't like dates without days
         date_complete = as.Date(paste("01",date), "%d %m %Y"), 
         # making sure numeric variables are treated as numbers
         month = as.numeric(as.character(month)), 
         year = as.numeric(as.character(year)),
         coal_size = as.numeric(as.character(coal_size)),
         pow_conc = as.numeric(as.character(pow_conc)),
         pow_conc2 = as.numeric(as.character(pow_conc2)),
         ideo_heter = as.numeric(as.character(ideo_heter)),
         icn = as.numeric(as.character(icn)),
         # Creating abs_dist variables
         abs_dist_pres_con = abs(ideo_dist_pres_con),
         abs_dist_pres_coal = abs(ideo_dist_pres_coal),
         abs_dist_coal_con = abs(ideo_dist_coal_con),
         effort_coal = abs_dist_pres_con - abs_dist_coal_con,
         coal_PIE=((coal_expen/tot_expen)+((tot_min_parties-cont_pres)/tot_min_parties))/2,
         pres_PIE=((prep_expen/tot_expen)+(cont_pres/tot_min_parties))/2,
         term=ordered(
           case_when(
             term=="Sarney" ~ 1,
             term=="Collor" ~ 2,
             term=="Itamar" ~ 3,
             term=="Cardoso 1" ~ 4,
             term=="Cardoso 2" ~ 5,
             term=="Lula 1" ~ 6,
             term=="Lula 2" ~ 7,
             term=="Rousseff 1" ~ 8,
             term=="Rousseff 2" ~ 9,
             term=="Temer 1" ~ 10),
           levels=c(1:10),
           labels=c("Sarney","Collor","Itamar",
                    "Cardoso 1","Cardoso 2","Lula 1","Lula 2",
                    "Rousseff 1","Rousseff 2","Temer 1")
         )) %>% 
  
  dplyr::select(date_complete,date_yearmon,year,month,president,term,partypresid,experience,
                effort_coal,abs_dist_pres_con,abs_dist_coal_con, # Ideology
                ideo_dist_pres_con,ideo_dist_pres_coal,ideo_dist_coal_con, # Ideology
                #coalition_name,
                tot_expen,tot_amen,cabinet_size,
                cham_presp2,coal_seat,coal_prop,# Nominal support seats
                cab_coal_OAM,cab_coal,cab_coal_gall,cab_coal_altern, # Proportionality measures 
                coal_size,qtt_part,pow_conc,ideo_heter,
                pow_conc2,icn,enpp,av_cong_ideol,tot_part,
                gdp_m_real,coal_PIE,pres_PIE,
                pres_pop)


## Ideology data LA ----
govs_LA <- read_rds("data/govs_LA.rds")

## ICG ----
ICG.df <- read_rds("data/ICG_scores.rds") %>%
  # Bringing important variables from V8
  left_join(
    read_excel("data/dataset_coalmngm_brazil_v8.xlsx",
               col_names = TRUE, skip = 6) %>%
      dplyr::filter(is.na(term)==F) %>%
      # Mutating and correcting lots of variables  
      mutate(date = as.character(date), # Making dates readable
             date = str_pad(date, 7, pad = "0"), #padding zeros to correct
             date_yearmon = as.yearmon(date,format="%m %Y"), # Using package to have years and months
             # bulding complete dates with days because R doesn't like dates without days
             date_complete = as.Date(paste("01",date), "%d %m %Y"), 
             # making sure numeric variables are treated as numbers
             month = as.numeric(as.character(month)), 
             year = as.numeric(as.character(year)),
             coal_size = as.numeric(as.character(coal_size)),
             pow_conc = as.numeric(as.character(pow_conc)),
             pow_conc2 = as.numeric(as.character(pow_conc2)),
             ideo_heter = as.numeric(as.character(ideo_heter)),
             icn = as.numeric(as.character(icn)),
             # Creating abs_dist variables
             abs_dist_pres_con = abs(ideo_dist_pres_con),
             abs_dist_pres_coal = abs(ideo_dist_pres_coal),
             abs_dist_coal_con = abs(ideo_dist_coal_con),
             effort_coal = abs_dist_pres_con - abs_dist_coal_con,
             coal_PIE=((coal_expen/tot_expen)+((tot_min_parties-cont_pres)/tot_min_parties))/2,
             pres_PIE=((prep_expen/tot_expen)+(cont_pres/tot_min_parties))/2) %>% rowwise %>%
      mutate(coalition_name=paste(coalpart_DEM,	coalpart_PAN,	coalpart_PCdoB,	coalpart_PDC,	
                                  coalpart_PDS,	coalpart_PDT,	coalpart_PEN,	coalpart_PHS,	
                                  coalpart_PL,	coalpart_PMDB,	coalpart_PMN,	coalpart_PP,	
                                  coalpart_PPL,	coalpart_PPS,	coalpart_PR,	coalpart_PRB,	
                                  coalpart_PRN,	coalpart_PROS,	coalpart_PRP,	coalpart_PRTB,	
                                  coalpart_PSB,	coalpart_PSC,	coalpart_PSD,	coalpart_PSDB,	
                                  coalpart_PSDC,	coalpart_PSL,	coalpart_PSOL,	coalpart_PST,	
                                  coalpart_PSTU,	coalpart_PT,	coalpart_PTB,	coalpart_PTC,	
                                  coalpart_PTdoB,	coalpart_PTN,	coalpart_PV,	coalpart_REDE,	
                                  coalpart_SD),
             coalition_name=str_sub(gsub(" ","",gsub("NA","",coalition_name)),1,-2)) %>% ungroup %>%
      dplyr::select(date_complete,date_yearmon,year,month,president,term,partypresid,experience,
                    effort_coal,abs_dist_pres_con,abs_dist_coal_con, # Ideology
                    ideo_dist_pres_con,ideo_dist_pres_coal,ideo_dist_coal_con, # Ideology
                    coalition_name,
                    tot_expen,tot_amen,cabinet_size,
                    cham_presp2,coal_seat,coal_prop,# Nominal support seats
                    cab_coal_OAM,cab_coal,cab_coal_gall,cab_coal_altern, # Proportionality measures 
                    coal_size,qtt_part,pow_conc,ideo_heter,
                    pow_conc2,icn,enpp,av_cong_ideol,tot_part,
                    gdp_m_real,coal_PIE,pres_PIE,
                    pres_pop) 
  ) %>%
  mutate(gdp_log=log(gdp_m_real+1)) %>%
  mutate(ICG.GDP=scales::rescale(ICG100/gdp_log, to = c(10,100)))

# Smoothing the time series
x <- decompose(ts(ICG.df$coal_PIE,start=c(1995, 1), end=c(2016, 12),frequency=12))$trend
ICG.df$coal_PIE = imputeTS::na.locf(x)

ICG.df <- ICG.df %>%
  mutate(pres_PIE=1-coal_PIE,
         ICG_pres=ICG100*pres_PIE,
         ICG_coal=ICG100*coal_PIE) %>%
  mutate(term=gsub("Temer 1","Temer",term)) 


mandatos <- left_join(ICG.df %>%
                        group_by(term) %>%
                        summarise(
                          start=first(date_yearmon),
                          end=last(date_yearmon)), 
                      ICG.df %>%
                        group_by(term) %>%
                        summarise_each(funs(if(is.numeric(.)) mean(., na.rm = TRUE) else first(.)))
) %>% dplyr::select(-date_yearmon) %>%
  mutate(term=gsub("Temer 1","Temer",term))


# Figures ----

## Fig 1 ----
fig1 <-
  govs_LA %>%
  ggplot() +
  geom_point(aes(x=div.gov,
                 y=reorder(paste0(ifs,str_sub(year,-2)),div.gov),
                 colour=(ifs!="bra")),
             family = "Palatino",size=3,
             fontface = "bold") +
  scale_color_grey() +
  theme_bw(base_family = "Palatino", base_size = 10) +
  theme(legend.background = element_blank(),
        legend.key = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        strip.background = element_rect(fill="white", color = "white"),
        plot.background = element_blank(),
        axis.line = element_blank(),
        axis.title.x = element_text(size=8),
        panel.grid.major.x = element_line(size = 0.1),
        panel.grid.major.y = element_line(size = 0.7),
        panel.grid.minor = element_blank(),
        legend.position="none",
        legend.title=element_blank(),
        axis.ticks = element_blank())+
  facet_wrap(vars(allhouse.cat),ncol=3 ,scales = "free_y") +
  geom_vline(xintercept = 0,linetype=2) +
  labs(x="Ideological differences",y="",title="") 

ggsave(plot=fig1,"figures/figure_1.pdf",device = "pdf",width = 6,height = 7,dpi = "retina")

## Fig 2 ----
fig2 <-
  coalmngm_brazil %>% 
  mutate(term=gsub("Temer 1","Temer",term)) %>%
  ggplot(aes(x=date_yearmon,y=ideo_dist_coal_con,group=term,colour=term)) + 
  geom_line(size=1.5) + 

  geom_hline(yintercept=0,color="grey50",size=1) +

  geom_text(data = . %>% ungroup() %>%
              group_by(term) %>% 
              mutate(start=min(date_yearmon)) %>%
              dplyr::filter(abs(ideo_dist_coal_con)==max(abs(ideo_dist_coal_con))) %>% 
              dplyr::select(term,ideo_dist_coal_con,start) %>% distinct(),
            aes(x = start, y = ideo_dist_coal_con, label = term,vjust=-1*(ideo_dist_coal_con/abs(ideo_dist_coal_con))),
            size = 4,hjust=0,family="Palatino",fontface="bold") +
  scale_x_yearmon("",format = "%Y") +
  scale_y_continuous("Ideological Difference",limits = c(-3,3)) +
  scale_colour_manual(values = rep(c("grey50","black"),6)) +
  theme_bw(base_family = "Palatino", base_size = 11) +
  theme(legend.background = element_blank(),
        legend.key = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        strip.background = element_blank(),
        plot.background = element_blank(),
        axis.line = element_blank(),
        panel.grid.major.x = element_line(size = 0.1),
        panel.grid.major.y = element_line(size = 0.7),
        panel.grid.minor = element_blank(),
        legend.position="none",
        legend.title=element_blank(),
        axis.ticks = element_blank())+
  ggtitle("")  

ggsave(plot=fig2,"figures/figure_2.pdf",device = "pdf",width = 8,height = 6,dpi = "retina")


## Fig 3 ----
fig3 <- 
  ICG.df %>% 
  dplyr::select(date_yearmon,ICG_coal,ICG_pres) %>% 
  tidyr::gather(var,value,2:3) %>%
  ggplot() +
  geom_rect(data=mandatos,aes(xmin = start, xmax = end, ymin = -Inf, ymax = Inf, fill = term), 
            alpha = 0.10) +
  geom_area(aes(y=value,x=date_yearmon,
                fill=reorder(var,desc(var))),
            alpha=0.7,position = "stack") +
  geom_text(data = mandatos,family="Palatino",
            aes(x = start, y = 100, label = term),
            size = 3,angle = 90,vjust=1,hjust=0.75) +
  scale_x_yearmon("",format = "%Y",n=6) +
  scale_y_continuous("Governing Costs") +
  scale_fill_manual(name="",values = rep(c("grey30","grey70"),5),
                    breaks=c("ICG_coal","ICG_pres"),
                    labels=c("GCI Coalition", "GCI President")) +
  theme_minimal(base_family = "Palatino", base_size = 11) +
  theme(legend.position = "top",
        plot.margin=unit(c(0,0,0,0), "mm")) 

ggsave(plot=fig3,"figures/figure_3.pdf",device = "pdf",width = 8,height = 5,dpi = "retina")


# Tables ----





